package cn.edu.scau.controller;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.config.Audience;
import cn.edu.scau.entity.Customer;
import cn.edu.scau.entity.Manager;
import cn.edu.scau.entity.Nurse;
import cn.edu.scau.entity.RecoveryRegister;
import cn.edu.scau.service.CustomerService;
import cn.edu.scau.service.ManagerService;
import cn.edu.scau.service.NurseService;
import cn.edu.scau.service.RecoveryRegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "康复登记控制器")
@RestController
@Controller
@RequestMapping("recoveryRegister")
public class RecoveryRegisterController {
	@Autowired
	RecoveryRegisterService recoveryRegisterService;
	@Autowired
	NurseService nurseService;
	@Autowired
	CustomerService customerService;
	@Autowired
	ManagerService managerService;
	@Autowired
	private Audience audience;//?
	
	/**
	 * 显示所有康复登记信息
	 * @return
	 */
	@ApiOperation(value = "显示所有信息页面:数据存储在hospital中")
    @GetMapping(value = "/list")
    public ResultBean list(){
        List<RecoveryRegister> recoveryRegistes = recoveryRegisterService.selectAll();
        System.out.println("请求所有登记数据："+recoveryRegistes);
        ResultBean r = new ResultBean(200,true,"查询所有登记信息成功",recoveryRegistes);
        return r;
    }
	
	/**
	 * 根据护士编号选择康复登记信息
	 * @return
	 */
	@ApiOperation(value = "显示所有信息页面:数据存储在hospital中")
    @GetMapping(value = "/nurseList/{userAccount}")
    public ResultBean nurseList(@PathVariable("userAccount") String userAccount){
		Manager manager = managerService.selectByUserAccount(userAccount);
		Nurse nurse = nurseService.selectByUserId(manager.getUserId());
        List<RecoveryRegister> recoveryRegistes = recoveryRegisterService.selectByNurseID(nurse.getNurseid());
        System.out.println("请求所有登记数据："+recoveryRegistes);
        ResultBean r = new ResultBean(200,true,"查询所有登记信息成功",recoveryRegistes);
        return r;
    }
	
	/**
	 * 根据主键查询康复登记
	 * @param recoveryRegisterID
	 * @return
	 */
	@GetMapping(value = "/{id}")
	public ResultBean findByID(@PathVariable("id") Integer recoveryRegisterID){
		ResultBean r = null;
		RecoveryRegister recoveryRegiste = recoveryRegisterService.selectByPrimaryKey(recoveryRegisterID);
		if(recoveryRegiste==null) {
			r = new ResultBean(500,false,"查询登记信息失败",null);
		}
		else {
			r = new ResultBean(200,true,"查询登记信息成功",recoveryRegiste);
		}
		return r;
	}
	
	/**
	 * 根据前端发来的护士编号检测该护士是否存在
	 * @param nurseid
	 * @return
	 */
	@GetMapping(value = "/checkNurseID/{nurseid}")
	public ResultBean checkNurseID(@PathVariable("nurseid") Integer nurseid){
		ResultBean r = null;
		Nurse nurse = nurseService.selectByPrimaryKey(nurseid);
		if(nurse==null) { 
			r = new ResultBean(500,false,"找不到编号为"+nurseid+"的护士，请确认后重新输入",null);
			return r;
		}
		else {
			if(nurse.getNurseuseflag()==false) {
				r = new ResultBean(500,false,"已经没有编号为"+nurseid+"的护士，请确认后重新输入",null);
				return r;
			}
		}
		r = new ResultBean(200,true,"护士编号检查成功",nurse.getNursename());
		return r;
	}
	
	/**
	 * 根据前端发来的客户编号检测该客户是否存在
	 * @param customerid
	 * @return
	 */
	@GetMapping(value = "/checkCustomerID/{customerid}")
	public ResultBean checkCustomerID(@PathVariable("customerid") Integer customerid){
		ResultBean r = null;
		Customer customer = customerService.selectByPrimaryKey(customerid);
		if(customer==null) { 
			r = new ResultBean(500,false,"找不到编号为"+customerid+"的客户，请确认后重新输入",null);
			return r;
		}
		else {
			if(customer.getCustomeruseflag()==false) {
				r = new ResultBean(500,false,"已经没有编号为"+customerid+"的客户，请确认后重新输入",null);
				return r;
			}
		}
		r = new ResultBean(200,true,"客户编号检查成功",customer.getCustomername());
		return r;
	}
	
	/**
	 * 新增康复登记
	 * @param recoveryRegister
	 * @param bindingResult
	 * @return
	 */
    @PostMapping(value = "/insert")
    public ResultBean insert(@Validated RecoveryRegister recoveryRegister, BindingResult bindingResult) {
        ResultBean r = null;
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError f:bindingResult.getFieldErrors()
                 ) {
                msg.append(f.getField()+":"+f.getDefaultMessage()+"\n");
            }
            r = new ResultBean(5006,false,msg.toString(),null);
            return r;
        }
        
        Nurse nurse=nurseService.selectByPrimaryKey(recoveryRegister.getNurseID());
        if(nurse==null) {
        	r =new ResultBean(5005,false,"护士编号不存在或无效",null);
        	return r;
        }
        else {
        	if(nurse.getNurseuseflag()==false) {
        		r =new ResultBean(5005,false,"护士编号不存在或无效",null);
        		return r;
        	}
        }//外键约束
        Customer customer = customerService.selectByPrimaryKey(recoveryRegister.getCustomerID());
        if(customer==null) {
        	r =new ResultBean(5005,false,"客户编号不存在或无效",null);
        	return r;
        }
        else {
        	if(customer.getCustomeruseflag()==false) {
        		r =new ResultBean(5005,false,"客户编号不存在或无效",null);
        		return r;
        	}
        }//外键约束
        
        boolean flag = recoveryRegisterService.insert(recoveryRegister);
        if(flag){
            r =new ResultBean(200,true,"增加成功\n新增康复登记编号为"+recoveryRegister.getRecoveryRegisterID(),null);
        }else
        {
            r =new ResultBean(5005,false,"增加失败",null);
        }
        return r;
    }
    
    /**
     * 修改康复登记
     * @param recoveryregisterid
     * @param recoveryRegister
     * @param bindingResult
     * @return
     */
    @PutMapping(value = "/update/{id}")
    public ResultBean update(@PathVariable("id") Integer recoveryregisterid,@Validated RecoveryRegister recoveryRegister, BindingResult bindingResult) {
        ResultBean r = null;
        if(bindingResult.hasErrors()){
            StringBuffer msg = new StringBuffer();
            for (FieldError f:bindingResult.getFieldErrors()
            ) {
                msg.append(f.getField()+":"+f.getDefaultMessage()+"\n");
            }
            r = new ResultBean(5006,false,msg.toString(),null);
            return r;
        }
       
        Nurse nurse=nurseService.selectByPrimaryKey(recoveryRegister.getNurseID());
        if(nurse==null) {
        	r =new ResultBean(5005,false,"护士编号不存在或无效",null);
        	return r;
        }
        else {
        	if(nurse.getNurseuseflag()==false) {
        		r =new ResultBean(5005,false,"护士编号不存在或无效",null);
        		return r;
        	}
        }//外键约束
        Customer customer = customerService.selectByPrimaryKey(recoveryRegister.getCustomerID());
        if(customer==null) {
        	r =new ResultBean(5005,false,"客户编号不存在或无效",null);
        	return r;
        }
        else {
        	if(customer.getCustomeruseflag()==false) {
        		r =new ResultBean(5005,false,"客户编号不存在或无效",null);
        		return r;
        	}
        }//外键约束
        
        boolean flag = recoveryRegisterService.update(recoveryRegister);
        if(flag){
            r =new ResultBean(200,true,"修改成功",null);
        }else
        {
            r =new ResultBean(5005,false,"修改失败",null);
        }
        return r;
    }
    
    /**
     * 根据主键删除康复登记
     * @param recoveryregisterid
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public ResultBean delete(@PathVariable("id") Integer recoveryregisterid){
        ResultBean r = null;
        boolean flag = recoveryRegisterService.delete(recoveryregisterid);
        if(flag){
            r =new ResultBean(200,true,"删除成功",null);
        }else
        {
            r =new ResultBean(5005,false,"删除失败",null);
        }
        return r;
    }
    
    /**
     * 根据康复登记编号、护士编号、护士姓名、客人编号、客户名组合查询
     * @param recoveryRegisterID
     * @param nurseID
     * @param nurseName
     * @param customerID
     * @param customerName
     * @return
     */
    @GetMapping(value = "/find")
    public ResultBean find(Integer recoveryRegisterID, Integer nurseID, String nurseName, Integer customerID, String customerName){
        List<RecoveryRegister> recoveryRegisters = recoveryRegisterService.find(recoveryRegisterID, nurseID, nurseName, customerID, customerName);
        ResultBean r = new ResultBean(200,true,"组合模糊查询登记信息成功",recoveryRegisters);
        return r;
    }
    
    /**
     * 根据康复登记编号、客人编号、客户名组合查询，护士编号已确定
     * @param userID
     * @param recoveryRegisterID
     * @param customerID
     * @param customerName
     * @return
     */
    @GetMapping(value = "/nurseFind/{userAccount}")
    public ResultBean nurseFind(@PathVariable("userAccount") String userAccount, Integer recoveryRegisterID, Integer customerID, String customerName){
    	Manager manager = managerService.selectByUserAccount(userAccount);
    	Nurse nurse = nurseService.selectByUserId(manager.getUserId());
    	List<RecoveryRegister> recoveryRegisters = recoveryRegisterService.findWithNurseID(recoveryRegisterID, nurse.getNurseid(), customerID, customerName);
        ResultBean r = new ResultBean(200,true,"组合模糊查询登记信息成功",recoveryRegisters);
        return r;
    }
}
